let actionHandlers = {};

//调用方法动态引入js文件,需要对应action的名称,需要时才会引入,避免每次都引入把它储存在actionHandlers中
const getActionHandler = async (action) => {
  if (!actionHandlers[action]) {
    try {
		console.log(`./${action}.js`);
      actionHandlers[action] = require(`@/services/socket/${action}.js`).default;
    } catch (error) {
		console.log(error);
      actionHandlers[action] = () => {
        console.warn(`No handler defined for action ${action}`);
      };
    }
  }
  return actionHandlers[action];
};


export default {
  async receive(data) {
    let res = String.fromCharCode.apply(null, new Uint8Array(data.data));
    res = JSON.parse(res);
    if (res.action !== 'heart') {
      console.log('socket消息--------------',res);
	  try {
	    const handler = await getActionHandler(res.action);
	    handler(res);
	  } catch (error) {
	    console.error(`Error handling action ${res.action}:`, error);
	  }
    }
  }
};
